package fiuba.webapp.action;

import fiuba.model.Configuracion;
import fiuba.model.RssConfig;
import fiuba.model.ContenidoEstatico;
import fiuba.model.Noticia;
import fiuba.service.GenericManager;
import fiuba.service.NoticiaManager;
import fiuba.webapp.action.HtmlEditorCommandsAction.FileInfo;

import java.util.ArrayList;
import java.util.List;
import java.util.Iterator;

public class RssAction extends BaseAction{
	/**
	 * 
	 */
	private static final long serialVersionUID = 4799331871097394769L;
	private NoticiaManager noticiaManager;
	private GenericManager<Configuracion, Long> configuracionManager;
	private GenericManager<RssConfig, Long> rssConfigManager;
	
	private String title;
	private String link;
	private String description;
	private Long id;

	// Para devolver los items de RSS
	public class rssItem {
		private String title;
		private String link;
		private String description;
		rssItem(String title, String link, String description) {
			this.title = title;
			this.link = link;
			this.description = description;
		}
		public String getTitle() { return title;}
		public String getLink() {return link;}
		public String getDescription() {return description;}
	}
    private List <rssItem> rssItems = new ArrayList<rssItem>();
	
	
	public void setNoticiaManager(NoticiaManager noticiaManager) {
		this.noticiaManager = noticiaManager;
	}
	public void setConfiguracionManager(
			GenericManager<Configuracion, Long> configuracionManager) {
		this.configuracionManager = configuracionManager;
	}
	
	public String rss() {
		Configuracion config = this.configuracionManager.get(1L);
		RssConfig rssConfig ;
		try {
			rssConfig = this.rssConfigManager.get(this.id);
		}
		catch (Exception e) {
			rssConfig = this.rssConfigManager.get(1L);
		}

		this.link = config.getUrl();
		this.title = rssConfig.getTitle();;
		this.description = rssConfig.getDescription();
		int largo = rssConfig.getTextLength(); 
		int maxitems = rssConfig.getMaxItems();
		
		// Si el link no tiene "/" al final, ponersela
		if (this.link.charAt(this.link.length() - 1) != '/') {
			this.link = this.link + "/";
		}
		List<Noticia> noticias ;
		if (rssConfig.getSecciones() == null || rssConfig.getSecciones().equals("")) {
			noticias = this.noticiaManager.findConLimit(1, maxitems);
		}
		else {
			noticias = this.noticiaManager.findConLimitYSeccion(1, maxitems,rssConfig.getSecciones());
		}
		Iterator i = noticias.iterator();
		while(i.hasNext()) { // Para cada noticia
			 Noticia n = (Noticia) i.next();
			 // Quito el HTML
			 String contenido = n.getContenido().replaceAll("\\<.*?>","");
			 // Si quedo mayor al largo permitido, quito hasta la ultima palabra y agrego "..."
			 if (contenido.length() > largo) {
				 int lastpos = largo;
				 if (!contenido.substring(largo,largo + 1).equals(" ") ) {
					 lastpos = contenido.lastIndexOf(" ",largo);
				 }
				 try {
					 contenido = contenido.substring(0,lastpos) + "...";
				 }
				 catch (Exception e) {
					 log.debug("ERROR Al trimear la noticia");
				 }
			 }
			 rssItems.add(new rssItem(n.getTitulo()
					 , this.link + "verNoticia.html?id=" + n.getId()
					 ,contenido));
		}		
		return SUCCESS;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getLink() {
		return link;
	}

	public void setLink(String link) {
		this.link = link;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}
	public List<rssItem> getRssItems() {
		return rssItems;
	}
	public void setRssItems(List<rssItem> rssItems) {
		this.rssItems = rssItems;
	}
	public void setRssConfigManager(GenericManager<RssConfig, Long> rssConfigManager) {
		this.rssConfigManager = rssConfigManager;
	}
	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}


}
